/**
 * 
 */
package cn.edu.cdtu.healthserver.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.cdtu.healthserver.dao.ChooseLogDao;
import cn.edu.cdtu.healthserver.dao.HealthFileBodyDao;
import cn.edu.cdtu.healthserver.dao.TestLogDao;
import cn.edu.cdtu.healthserver.domain.Choose;
import cn.edu.cdtu.healthserver.domain.ChooseLog;
import cn.edu.cdtu.healthserver.domain.HealthFileBody;
import cn.edu.cdtu.healthserver.domain.Test;
import cn.edu.cdtu.healthserver.domain.TestLog;
import cn.edu.cdtu.healthserver.domain.UserInformation;
import cn.edu.cdtu.healthserver.utils.C3P0Util;

/**
 * @author zheng
 *
 */
public class HealthFileBodyDaoImplementation implements HealthFileBodyDao {

	QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());

	TestLogDao testLogDao = new TestLogDaoImplementation();
	ChooseLogDao chooseLogDao = new ChooseLogDaoImplementation();
	
	
	
	/* (non-Javadoc)
	 * @see cn.edu.scu.healthserver.dao.HealthFileBodyDao#getBodyLastTestLog(java.util.List)
	 * 获取最近的身体测试testlog
	 */
	@Override
	public TestLog getBodyLastTestLog(List<TestLog> testLogs) {
		// TODO Auto-generated method stub
		for (TestLog testLog : testLogs) {
			if (isBodyTest(testLog)) {
				return testLog;
			}
		}
		return null;
	}
	
	
	//获取最近测试testlog中有分值的choose
	@Override
	public List<Choose> getChoose(List<ChooseLog> chooseLogs) {
		// TODO Auto-generated method stub
		List<Choose> chooses = new ArrayList<Choose>();
		
		for(ChooseLog chooseLog:chooseLogs){
			String sql = "select * from choose where id="+chooseLog.getChoose_id();
			try {
				Choose choose = qr.query(sql, new BeanListHandler<Choose>(Choose.class)).get(0);
				if(choose.getPoint()>0){
					chooses.add(choose);
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		return chooses;
	}

	@Override
	public HealthFileBody getHealthFileBodyAnswer(List<Choose> chooses) {
		// TODO Auto-generated method stub
		 int vitaminA=0;
		 int vitaminB=0;
		 int vitaminC=0;
		 int vitaminE=0;
		 int lecithin=0;
		 int calcium=0;
		 int aminoAcid=0;
		 int TraceElements=0;
		
		 for(Choose choose:chooses){
			 String sql = "select * from answer_body where question_id="+choose.getQuestion_id();
			 try {
				HealthFileBody fileBody=qr.query(sql, new BeanListHandler<HealthFileBody>(HealthFileBody.class)).get(0);
				vitaminA+=fileBody.getVitaminA();
				vitaminB+=fileBody.getVitaminB();
				vitaminC+=fileBody.getVitaminC();
				vitaminE+=fileBody.getVitaminE();
				lecithin+=fileBody.getLecithin();
				calcium +=fileBody.getCalcium();
				aminoAcid+=fileBody.getAminoAcid();
				TraceElements+=fileBody.getTraceElements();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new RuntimeException();
			}
		 }
		 
		 HealthFileBody body = 
				 new HealthFileBody(vitaminA,vitaminB,vitaminC,
						 vitaminE,lecithin,calcium,aminoAcid,TraceElements);
		 return body;

	
	}

	@Override
	public HealthFileBody getHealthFileBody(UserInformation user) {
		// TODO Auto-generated method stub
		
		List<TestLog> testLogs = testLogDao.query(user);
		
		TestLog lastTestLog = getBodyLastTestLog(testLogs);
		
		List<ChooseLog> chooseLogs = chooseLogDao.query(lastTestLog);
		
		List<Choose> chooses = getChoose(chooseLogs);
		
		HealthFileBody healthFileBody = getHealthFileBodyAnswer(chooses);
		
		return healthFileBody;
		
	}

	// 判断是哪种测试类型
	public Boolean isBodyTest(TestLog testLog) {

		String sql = "select * from test where id = " + testLog.getTest_id();

		try {
			if (qr.query(sql, new BeanListHandler<Test>(Test.class)).get(0)
					.getType_id() == 2) {
				return true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException();
		}
		return false;
	}

}
